package shuju;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Prep {
	static Connection con;
	static PreparedStatement sql;
	static ResultSet res;
	public Connection GetConnection(){
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con=DriverManager.getConnection("jdbc:mysql:"+"//127.0.0.1:3306/test","root","123456");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return con;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Prep c=new Prep();
		con=c.GetConnection();
		try {
			sql=con.prepareStatement("select * from tb_stu"+"where id=?");
			sql.setObject(1, 2);
			res=sql.executeQuery();
			while(res.next()){
				String id=res.getString("id");
				String name=res.getString("name");
				System.out.println(id);
				System.out.println(name);
				System.out.println("");
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
				

	}

}
